//
//  CantripWorldController.h
//  Cantrip
//
//  Created by James Callender on Wed Apr 24 2002.
//  Copyright (c) 2001 James Callender. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import <CantripSDK/CantripSDK.h>
//#import <FScript/FScript.h>
#import "CantripAppController.h"
#import "CantripScriptManager.h"
#import "CantripWorldDocument.h"
#import "CantripInputField.h"
#include "mccpDecompress.h"

#define IAC	    255
#define DONT	    254
#define DO	    253
#define WONT	    252
#define WILL	    251
#define SB	    250
#define GA	    249
#define EL	    248
#define EC	    247
#define IP	    244
#define BRK	    243
#define NOP	    241
#define SE	    240

// options
#define TRANSMIT_BINARY     0
#define ECHO		    1
#define SUPPRESS_GO_AHEAD   3
#define STATUS		    5
#define TERMINAL_TYPE       24
#define END_OF_RECORD       25
#define NAWS		    31
#define TERMINAL_SPEED      32
#define REMOTE_FLOW_CONTROL 33
#define LINEMODE	    34
#define X_DISPLAY_LOCATION  35
#define AUTHENTICATION      37
#define ENCRYPT		    38
#define ENVIRONMENT	    39
#define MSP		    90
#define MXP		    91

@class CantripWorldDocument;

@interface CantripWorldController : NSWindowController
{
    IBOutlet CantripDisplayView	*oTermView;
    IBOutlet NSTextField	*oStatusBar;
    IBOutlet NSTextField	*oMsgBar;
    IBOutlet CantripInputField  *oInputField;
    IBOutlet NSSplitView	*oDividerView;
    IBOutlet NSPanel		*oAddressPanel;
    IBOutlet NSTextField	*oAddressPanelAddressField;
    IBOutlet NSTextField	*oAddressPanelPortField;
    IBOutlet NSPanel		*oLoggingPanel;
    IBOutlet NSTextField	*oLoggingPanelFileField;
    IBOutlet NSScrollView       *oInputScrollView;
    IBOutlet NSProgressIndicator *oProgressWidget;
    IBOutlet NSView		*oLogOptionsView;
    IBOutlet NSButton		*oLogRetroactiveButton;
    IBOutlet NSPanel		*oPasswordPanel;
    IBOutlet NSSecureTextField  *oPasswordField;
    
    NSToolbarItem   *connectButtonItem;
    
    NSConnection    *mThreadConnection;
    NSThread        *mMyThread;
    
    // Socket information
    CantripSocket   *mSocket;
    unsigned int    mState;
    
    // Mud Client Compression Protocol
    BOOL            handlingMCCP;
    mc_state        *mccp_state;
    
    // Telnet protocol info
    int		IAC_STATE;
    int		IAC_SB;
    int		IAC_SB_TYPE;
    
    // Terminal type sub-negotiation info
    int		ttrequest_type;
    int		ttindex;
    
    NSMutableDictionary     *mWindowData;
    NSMutableArray		*mCommandHistory;
    NSFileHandle		*mLogFileHandle;
    BOOL                    isLogging;
    int                     mHistoryLocation;
    
    NSTimer                 *mReconnectionTimer;
    int                     _reconnectionTries;
    
    int beatCount;
    
    NSString *uniqueID;
    int childNumber;
}

- (BOOL)keyDownInWindow:(NSEvent *)event;

- (BOOL)startLogForFile:(NSString *)filePath retroactive:(BOOL)retroactive;
- (void)setupToolbar;
- (void)loadPreferences;

- (CantripWorldDocument *)world;

- (IBAction)connectButtonAction:(id)sender;
- (IBAction)settingsButtonAction:(id)sender;
- (IBAction)clearConsoleAction:(id)sender;
- (IBAction)toggleLoggingAction:(id)sender;
- (IBAction)addressPanelOKAction:(id)sender;
- (IBAction)addressPanelCancelAction:(id)sender;

- (void)moveHistoryLocation:(int)dir;

- (NSString *)controllerID;
- (NSMutableDictionary *)childData;
- (CantripSocket *)socket;
- (unsigned int)state;
- (CantripDisplayView*)termView;
- (CantripInputField *)inputField;
- (void)setMsgBar:(NSString *)msg;
- (NSString *)msgBar;

- (void)sendString:(NSString *)str;
- (void)echoString:(NSString *)str;
- (void)sendStringAndEcho:(NSString *)str;
- (void)messageString:(NSString *)str;

- (void)setState:(int)s;
- (void)setStateAndUpdate:(int)s;
- (void)connect;
- (void)disconnect;
- (void)connectThread:(NSArray *)ports;
- (void)handleConnectionEvent:(WorldConnectionEvent)event;
- (void)handleIACParsing:(NSMutableData *)data;
- (void)socketConnected:(CantripSocket*)inSocket;
- (void)socketDisconnected:(CantripSocket*)inSocket;
- (void)socket:(CantripSocket*)inSocket connectionTimedOut:(NSTimeInterval)inTimeout;
- (void)socket:(CantripSocket*)inSocket dataAvailable:(unsigned)inAmount;
- (void)socketSentData:(CantripSocket*)inSocket;

@end

void MakeUniformLineEndings(NSMutableString *string);
